package com.huadao.cart_ccs.experiment.log.protocolLog;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseListFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.experiment.log.OnDay;
import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.common.adapter.CommonRecycleAdapter;
import com.huadao.cart_ccs.common.adapter.CommonViewHolder;
import com.huadao.cart_ccs.common.adapter.FooterHolder;
import com.huadao.cart_ccs.common.adapter.HeaderHolder;
import com.huadao.cart_ccs.common.adapter.onItemCommonClickListener;
import com.huadao.cart_ccs.data.bo.Protocol;
import com.huadao.cart_ccs.dialog.DialogUtil;

import java.util.ArrayList;
import java.util.List;

public class ProtocolLogFrag extends BaseListFragment implements OnDay,ProtocolLogContract.IProtocolLogView{

    private ProtocolPresenter protocolPresenter;
    private List<Protocol> protocolList;

    public static ProtocolLogFrag getInstance(int day){
        Bundle bundle=new Bundle();
        bundle.putInt("day",day);
        ProtocolLogFrag protocolLogFrag=new ProtocolLogFrag();
        protocolLogFrag.setArguments(bundle);
        return protocolLogFrag;
    }

    @Override
    public int getRecycleViewId() {
        return R.id.recycleView;
    }

    @Override
    public int getSwipeRefreshLayoutId() {
        return R.id.swipeRefreshLayout;
    }

    @Override
    protected void init() {
        protocolPresenter=new ProtocolPresenter(this);
        Bundle bundle=getArguments();
        if(bundle!=null&&bundle.containsKey("day")){
            int day=bundle.getInt("day");
            protocolPresenter.setDay(day);
        }
        setEnableLoadMore(false);
        protocolList=new ArrayList<>();
    }

    @Override
    protected RecyclerView.ItemDecoration setItemDecoration() {
        return new DividerItemDecoration(this.getContext(), DividerItemDecoration.VERTICAL);
    }

    @Override
    protected CommonRecycleAdapter setAdapter() {
        return new ProtocolAdapter(mContext, protocolList, R.layout.item_protocol_log,R.layout.rv_footer);
    }

    @Override
    public RecyclerView.LayoutManager setLayoutManage() {
        return new LinearLayoutManager(this.getContext());
    }

    @Override
    protected void loadData() {
        protocolPresenter.getProtocolList();
    }

    @Override
    protected void loadMoreData() {
        protocolPresenter.getMoreProtocols();
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        protocolPresenter.getProtocolList();
    }

    @Override
    public int getContentViewId() {
        return R.layout.frag_list;
    }

    @Override
    protected BasePresenter getPresenter() {
        return protocolPresenter;
    }

    @Override
    public void showProtocolLogList(List protocolList) {
        this.protocolList.clear();
        this.protocolList.addAll(protocolList);
        notifyDataSetChanged();
        setSwipeRefresh(false);
    }

    @Override
    public void showMoreProtocolLogList(List<Protocol> protocolList) {
        if(protocolList==null||protocolList.size()==0){
            showToast("没有更多数据");
            return;
        }
        int start=this.protocolList.size();
        this.protocolList.addAll(protocolList);
        customeAdapter.notifyItemRangeInserted(start,protocolList.size());
    }

    @Override
    public void daySelected(int day) {
        if(day!=protocolPresenter.getDay()) {
            protocolPresenter.setDay(day);
            onRefresh();
        }
    }

    class ProtocolAdapter extends CommonRecycleAdapter<Protocol>{
        private DialogUtil dialogUtil;
        private onItemCommonClickListener commonClickListener = new onItemCommonClickListener() {
            @Override
            public void onItemClickListener(int position) {
                if(position==protocolList.size()+1){
                    protocolPresenter.getMoreProtocols();
                }else {
//                    DialogFragmentHelper.showMessageDialog(getActivity(),null,protocolList.get(position).toString());
                    dialogUtil.showMessageDialog(protocolList.get(position).toString(), null);
                }
            }

            @Override
            public void onItemLongClickListener(int position) {

            }
        };

        public ProtocolAdapter(Context context, List<Protocol> dataList, int layoutId, int footerLayoutId) {
            super(context, dataList, layoutId, footerLayoutId);
            dialogUtil=new DialogUtil(context);
        }

        @Override
        protected void bindData(CommonViewHolder holder, Protocol data) {
            holder.setText(R.id.tv_time, TimeUtil.log_sdf.format(data.getDate()));
            holder.setText(R.id.tv_protocol, data.toString());
            if(data.getType()==Protocol.type_request){
                holder.setText(R.id.tv_type,getString(R.string.request));
            }else if(data.getType()==Protocol.type_response){
                holder.setText(R.id.tv_type,getString(R.string.response));
            }
            holder.setCommonClickListener(commonClickListener);
        }

        @Override
        protected void bindFooterData(FooterHolder holder) {
            holder.setCommonClickListener(commonClickListener);
        }

        @Override
        protected void bindHeaderData(HeaderHolder holder) {

        }
    }
}
